<?php
class Sanpham_model extends CI_Model
{
    public $table = 'sanpham';
    public $table1 = 'category';

    function getOne($id)
    {
        return $this->db
            ->where('id', $id)
            ->get($this->table)
            ->row_array();
    }
    public function  getProductByCate($cat_ids)
    {
        $cat_ids = explode(',', $cat_ids);
        return $this->db
            ->where_in('cate_id', $cat_ids)
            ->limit(3)
            ->order_by('rand()')
            ->get($this->table)
            ->result_array();
    }
    public function  getProductByCate_1($cate_id)
    {
        return $this->db
            ->where('cate_id', $cate_id)
            ->limit(3)
            ->order_by('id', 'asc')
            ->get($this->table)
            ->result_array();
    }

    public function  getProductByCate_2($cate_id)
    {
        return $this->db
            ->where('cate_id', $cate_id)
            ->limit(3)
            ->order_by('id', 'asc')
            ->get($this->table)
            ->result_array();
    }

    public function  menu_left($cate_id)
    {
        $pageSize = 15;
        $page = @$_REQUEST['page'];
        $offset = $page * $pageSize;
        $cate_ids = array($cate_id);
        $rows = $this->db
            ->select('id')
            ->where('prent_id', $cate_id)
            ->get('category')
            ->result_array();
        foreach ($rows as $row) {
            $cate_ids[] = $row['id'];
        }
        return $this->db
            ->where_in('cate_id', $cate_ids)
            ->order_by('id', 'asc')
            ->limit($pageSize, $offset)
            ->get($this->table)
            ->result_array();
    } /*mua he*/
    public function  category($prent_id)
    {
        return $this->db
            ->where('prent_id', $prent_id)
            ->get($this->table1)
            ->result_array();
    }

    function getCatByParent($prent_id)
    {
        return $this->db
            ->where('prent_id', $prent_id)
            ->get($this->table1)
            ->result_array();
    }

    public function  muahe($prent_id)
    {
        return $this->db
            ->where('prent_id', $prent_id)
            ->where('id', 2)
            ->get($this->table1)
            ->result_array();
    }

    public function jointable($prent_id)
    {
        return $this->db
            ->select('*')
            ->from('sanpham')
            ->join('category', ' sanpham.cate_id = category.id')
            ->limit(8)
            ->order_by('sanpham.id', 'desc')
            ->where('prent_id', $prent_id)
            ->get()
            ->result_array();
    }
}